package com.dalin.system.service.imple;

import com.dalin.framework.dao.ConnectFactory;
import com.dalin.framework.dao.DbConn;
import com.dalin.framework.vos.JdbcVo;
import com.dalin.system.dao.DbColumnMapper;
import com.dalin.system.dao.DbManagementMapper;
import com.dalin.system.dao.DbTableMapper;
import com.dalin.system.pojo.entity.DbColumnEntity;
import com.dalin.system.pojo.entity.DbManagementEntity;
import com.dalin.framework.BaseServiceImple;
import com.dalin.framework.vos.PageInfoVo;
import com.dalin.framework.vos.ResResult;
import com.dalin.system.pojo.entity.DbTableEntity;
import com.dalin.system.pojo.request.DbColumnReq;
import com.dalin.system.pojo.request.DbManagementRequest;
import com.dalin.system.pojo.request.DbTableReq;
import com.dalin.system.service.DbManagementService;
import com.dalin.system.pojo.response.DbManagementVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DbManagementServiceImpl extends BaseServiceImple implements DbManagementService {
    @Autowired
    private DbManagementMapper dbManagementMapper;
    @Autowired
    private DbColumnMapper dbColumnMapper;
    @Autowired
    private DbTableMapper dbTableMapper;

    @Override
    public ResResult listPageDbManagements(DbManagementRequest req) {
        log.info("listPageDbManagements params:"+req.getPageNum() + "," + req.getPageSize());
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List l = dbManagementMapper.listDbManagements(req);
        PageInfoVo piv = new PageInfoVo(l);
        piv.setList(getDbManagementVos(l));
        return ResResult.getSuccessRes(piv);
    }
    public List<DbManagementVo> getDbManagementVos(List<DbManagementEntity> orderInfoList) {
        return orderInfoList.stream().map(entity -> {
            DbManagementVo vo = new DbManagementVo();
            BeanUtils.copyProperties(entity, vo);
            vo.setId(entity.getId()+"");
            //vo.setDbTypeDisplay(entity.getDbTypeDisplay());
            return vo;
        }).collect(Collectors.toList());
    }

    public Long addOrUpdateDbManagement(DbManagementEntity DbManagement) {
        if (DbManagement.getId() == null || DbManagement.getId() == 0) {
            dbManagementMapper.insert(DbManagement);
        } else {
            dbManagementMapper.updateById(DbManagement);

        }
        return DbManagement.getId();
    }



    public Long deleteDbManagement(Long id) {
        int i = dbManagementMapper.deleteById(id);
        System.out.println("dbManagementMapper.deleteById("+id+")return ===="+i);
        return id;
    }

    public void deleteDbManagements(List<String> ids) {
        int i = dbManagementMapper.deleteBatchIds(ids);
       // return id;
    }

    @Override
    public ResResult connectTest(DbManagementRequest req) {
        Map<String, Object> rtnMap = new HashMap<>();
        List<DbManagementEntity> list = dbManagementMapper.selectBatchIds(req.getIds());
        if (list == null || list.size() == 0) return ResResult
                .getFailureRes("数据库配置不存在",null);
        for (DbManagementEntity dbManagement : list) {
            JdbcVo jdbcVo = new JdbcVo();
            BeanUtils.copyProperties(dbManagement, jdbcVo);
            DbConn dc = ConnectFactory.getConnectionInstance(jdbcVo);
            if (dc.connectTest()) {
                rtnMap.put(dbManagement.getDbName()+"("+dbManagement.getDbIp()+")", "连接成功");
            } else {
                rtnMap.put(dbManagement.getDbName()+"("+dbManagement.getDbIp()+")", "连接失败");
            }
        }
        return ResResult.getSuccessRes(rtnMap);
    }

    @Override
    public ResResult addTable(DbTableReq req) {
        DbTableEntity dbTableEntity = new DbTableEntity();
        dbTableEntity.setTableName(req.getTableName());
        dbTableEntity.setTableComment(req.getTableComment());
        dbTableEntity.setDbId(req.getDbId());
        dbTableMapper.insert(dbTableEntity);
        List<DbColumnEntity> dbColumnEntities = req.getDbColumnReqList().stream().map(dbColumnReq -> {
            DbColumnEntity entity = new DbColumnEntity();
            BeanUtils.copyProperties(dbColumnReq, entity);
            entity.setTableId(dbTableEntity.getTableId());
            return entity;
        }).collect(Collectors.toList());
        dbColumnMapper.insertBatch(dbColumnEntities);
        return ResResult.getSuccessRes();
    }

    public DbManagementEntity getDbManagementById(int id) {
        return dbManagementMapper.selectById(id);
    }
}